package logic

import (
	"context"
	"errors"
	"gorm.io/gorm"
	"wx/models/mysql"

	"wx/wxrpc/internal/svc"
	"wx/wxrpc/wxrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateLogic) Update(in *wxrpc.UpdateRequest) (*wxrpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line
	var findUser mysql.User
	mysql.DB.Debug().Where("id=?", in.UserId).Find(&findUser)
	if in.UserId == 0 {
		return nil, errors.New("该用户不存在")
	}

	user := mysql.User{
		Model:     gorm.Model{ID: uint(in.UserId)},
		Mobile:    in.Mobile,
		HeadImage: in.HeadImage,
		Address:   in.Address,
	}
	mysql.DB.Debug().Updates(&user)

	return &wxrpc.UpdateResponse{
		UserId: int64(user.ID),
	}, nil
}
